﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TravianHelper.hero;
using TravianHelper.app;
using TravianHelper.village;
using System.Collections;

namespace TravianHelper
{
    public partial class LiteVersion : Form
    {
        public LiteVersion()
        {
            InitializeComponent();
            TravianApplicationContext context = new TravianApplicationContext();
        }

        private void buttonLogin_Click(object sender, EventArgs e)
        {
            TravianApplicationContext.adapter.username1 = textBoxUsername.Text;
            TravianApplicationContext.adapter.password1 = textBoxPassword.Text;
            if (TravianApplicationContext.loggedIn == false)
            {
                if (TravianApplicationContext.adapter.login())
                {
                    TravianApplicationContext.loggedIn = true;
                    buttonLogin.Text = "logout";
                    TravianApplicationContext.loadData();
                    MessageBox.Show("Logged in successful");
                }
                else
                {
                    MessageBox.Show("Log in failed");
                }
            }
            else
            {
                TravianApplicationContext.adapter.logout();
            }
        }

        private void LiteVersion_FormClosing(object sender, FormClosingEventArgs e)
        {
            Properties.Settings.Default.Save();
            Util.SerializeArrayList(TravianApplicationContext.oasis, "data.xml");
            Util.SerializeArrayList(TravianApplicationContext.farmlists, "farmlists.xml");
            Util.SerializeArrayList(TravianApplicationContext.villages, "villages.xml");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            doTick();
            timer1.Start();
        }

        private void doTick()
        {
            if (checkBoxDoQuest.Checked)
            {
                Task task = TravianApplicationContext.adapter.checkTask();
                if (task.number >= 5)
                {
                    HeroInfo hero = TravianApplicationContext.adapter.getHeroInfo();
                    if (checkBoxAdventure.Checked && hero.available && hero.health > 40)
                    {
                        TravianApplicationContext.adapter.adventure();
                    }

                    if (checkBoxAddPoint.Checked && hero.pointsToAdd > 0)
                    {
                        TravianApplicationContext.adapter.addFighting(hero.pointsToAdd);
                    }
                }
                if (task.completed)
                {
                    TravianApplicationContext.adapter.completeQuest(task.number);
                    Console.WriteLine("completed quest " + task.number);
                    doTick();
                }
                else
                {
                    switch (task.number)
                    {
                        case 1:
                            VillageInfo villageInfo = TravianApplicationContext.adapter.getVillageInfo("0");
                            if (!villageInfo.buildBusy)
                            {
                                String buildLink = TravianApplicationContext.adapter.buildLink("build.php?id=1", 0);
                                if (buildLink != "")
                                {
                                    TravianApplicationContext.adapter.build(buildLink);
                                    Console.WriteLine("Build woodcutter level 1");
                                    doTick();
                                    return;
                                }
                                else
                                {
                                    Console.WriteLine("Not enough res to build woodcutter level 1");
                                }
                            }
                            break;
                        case 2:
                            villageInfo = TravianApplicationContext.adapter.getVillageInfo("0");
                            if (!villageInfo.buildBusy)
                            {
                                String buildLink = TravianApplicationContext.adapter.buildLink("build.php?id=2", 0);
                                if (buildLink != "")
                                {
                                    TravianApplicationContext.adapter.build(buildLink);
                                    Console.WriteLine("Build crop land level 1");
                                    doTick();
                                    return;
                                }
                                else
                                {
                                    Console.WriteLine("Not enough res to build crop land level 1");
                                }
                            }
                            break;
                        case 3:
                            doTick();
                            break;
                        case 4:
                            villageInfo = TravianApplicationContext.adapter.getVillageInfo("0");
                            if (!villageInfo.buildBusy)
                            {
                                String buildLink = TravianApplicationContext.adapter.buildLink("build.php?id=39", 16);
                                if (buildLink != "")
                                {
                                    TravianApplicationContext.adapter.build(buildLink);
                                    Console.WriteLine("Build rally point level 1");
                                    doTick();
                                    return;
                                }
                                else
                                {
                                    Console.WriteLine("Not enough res to build rally point level 1");
                                }
                            }
                            break;
                        case 5:
                            HeroInfo hero = TravianApplicationContext.adapter.getHeroInfo();
                            if (checkBoxAdventure.Checked && hero.available && hero.health > 40)
                            {
                                TravianApplicationContext.adapter.adventure();
                                doTick();
                            }
                            break;
                        case 6:
                            doTick();
                            break;
                        case 7:
                            villageInfo = TravianApplicationContext.adapter.getVillageInfo("0");
                            if (!villageInfo.buildBusy && villageInfo.getLevel("build.php?id=4")==0)
                            {
                                String buildLink = TravianApplicationContext.adapter.buildLink("build.php?id=4", 0);
                                if (buildLink != "")
                                {
                                    TravianApplicationContext.adapter.build(buildLink);
                                    Console.WriteLine("Build iron mine level 1");
                                    return;
                                }
                                else
                                {
                                    Console.WriteLine("Not enough res to build iron mine level 1");
                                }
                            }
                            if (!villageInfo.buildBusy && villageInfo.getLevel("build.php?id=4") != 0 && villageInfo.getLevel("build.php?id=5") == 0)
                            {
                                String buildLink = TravianApplicationContext.adapter.buildLink("build.php?id=5", 0);
                                if (buildLink != "")
                                {
                                    TravianApplicationContext.adapter.build(buildLink);
                                    Console.WriteLine("Build clay pit level 1");
                                    doTick();
                                }
                                else
                                {
                                    Console.WriteLine("Not enough res to build clay pit level 1");
                                }
                            }
                            break;
                        case 8:
                            if (TravianApplicationContext.adapter.readTaskMessage())
                            {
                                Console.WriteLine("Read task message");
                                doTick();
                            }
                            break;
                        case 9:
                            villageInfo = TravianApplicationContext.adapter.getVillageInfo("0");
                            if (!villageInfo.buildBusy && villageInfo.getLevel("build.php?id=6") == 0)
                            {
                                String buildLink = TravianApplicationContext.adapter.buildLink("build.php?id=6", 0);
                                if (buildLink != "")
                                {
                                    TravianApplicationContext.adapter.build(buildLink);
                                    Console.WriteLine("Build clay pit level 1");
                                    doTick();
                                }
                                else
                                {
                                    Console.WriteLine("Not enough res to build clay pit level 1");
                                }
                            }
                            if (!villageInfo.buildBusy && villageInfo.getLevel("build.php?id=3") == 0)
                            {
                                String buildLink = TravianApplicationContext.adapter.buildLink("build.php?id=3", 0);
                                if (buildLink != "")
                                {
                                    TravianApplicationContext.adapter.build(buildLink);
                                    Console.WriteLine("Build woodcutter level 1");
                                    doTick();
                                }
                                else
                                {
                                    Console.WriteLine("Not enough res to build woodcutter level 1");
                                }
                            }
                            if (!villageInfo.buildBusy && villageInfo.getLevel("build.php?id=8") == 0)
                            {
                                String buildLink = TravianApplicationContext.adapter.buildLink("build.php?id=8", 0);
                                if (buildLink != "")
                                {
                                    TravianApplicationContext.adapter.build(buildLink);
                                    Console.WriteLine("Build crop land level 1");
                                    doTick();
                                }
                                else
                                {
                                    Console.WriteLine("Not enough res to build crop land level 1");
                                }
                            }
                            if (!villageInfo.buildBusy && villageInfo.getLevel("build.php?id=7") == 0)
                            {
                                String buildLink = TravianApplicationContext.adapter.buildLink("build.php?id=7", 0);
                                if (buildLink != "")
                                {
                                    TravianApplicationContext.adapter.build(buildLink);
                                    Console.WriteLine("Build iron mine level 1");
                                    doTick();
                                }
                                else
                                {
                                    Console.WriteLine("Not enough res to build iron mine level 1");
                                }
                            }
                            break;
                        }
                }
            }
            else
            {
                try
                {
                    HeroInfo hero = TravianApplicationContext.adapter.getHeroInfo();
                    if (checkBoxAdventure.Checked && hero.available && hero.health > 40)
                    {
                        TravianApplicationContext.adapter.adventure();
                    }

                    if (checkBoxAddPoint.Checked && hero.pointsToAdd > 0)
                    {
                        TravianApplicationContext.adapter.addFighting(hero.pointsToAdd);
                    }

                    VillageInfo villageInfo = TravianApplicationContext.adapter.getVillageInfo("0");
                    if (!villageInfo.buildBusy)
                    {
                        if (checkBoxUpCranny.Checked)
                        {
                            int maxStore = Math.Max(villageInfo.maxStore, villageInfo.maxCrop);
                            int maxCranny = TravianApplicationContext.adapter.getMaxCranny(textBoxCrannyID.Text);
                            if (maxCranny < maxStore)
                            {
                                String buildLink = TravianApplicationContext.adapter.linkBuild(textBoxCrannyID.Text, 23);
                                if (buildLink != "")
                                {
                                    TravianApplicationContext.adapter.build(buildLink);
                                    Console.WriteLine("Build cranny");
                                    return;
                                }
                                else
                                {
                                    Console.WriteLine("Not enough res to build cranny");
                                }
                            }
                        }

                        if (checkBoxUpRes.Checked)
                        {
                            ArrayList resFields = villageInfo.resFields;
                            for (int i = 0; i < resFields.Count; i++)
                            {
                                ResourceField res = (ResourceField)resFields[i];
                                String buildLink = TravianApplicationContext.adapter.buildLink(res.buildLink, 0);
                                if (buildLink != "")
                                {
                                    TravianApplicationContext.adapter.build(buildLink);
                                    Console.WriteLine("Build " + res.name + " level " + res.level);
                                    return;
                                }
                                else
                                {
                                    Console.WriteLine("Not enough res to build " + res.name + " level " + res.level);
                                }
                            }
                        }

                        if (checkBoxUpStore.Checked)
                        {
                            int maxRes = Math.Max(villageInfo.clay, Math.Max(villageInfo.iron, villageInfo.lumber));
                            if (maxRes > villageInfo.maxStore * 0.9)
                            {
                                String buildLink = TravianApplicationContext.adapter.linkBuild(textBoxStorageID.Text, 10);
                                if (buildLink != "")
                                {
                                    TravianApplicationContext.adapter.build(buildLink);
                                    Console.WriteLine("Build storage");
                                    return;
                                }
                                else
                                {
                                    Console.WriteLine("Not enough res to build storage");
                                }
                            }
                        }

                        if (checkBoxUpGranary.Checked)
                        {
                            if (villageInfo.crop > villageInfo.maxCrop * 0.9)
                            {
                                String buildLink = TravianApplicationContext.adapter.linkBuild(textBoxGranaryID.Text, 11);
                                if (buildLink != "")
                                {
                                    TravianApplicationContext.adapter.build(buildLink);
                                    Console.WriteLine("Build granary");
                                    return;
                                }
                                else
                                {
                                    Console.WriteLine("Not enough res to build granary");
                                }
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("Building something");
                    }

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.StackTrace);
                    TravianApplicationContext.adapter.login();
                    doTick();
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                doTick();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            VillageInfo villageInfo = TravianApplicationContext.adapter.getVillageInfo("0");
            MessageBox.Show(villageInfo.getLevel("build.php?id=4").ToString());
        }
    }
}
